package com.RecipeBook.cooking;

import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

import com.RecipeBook.browse.DataBaseHelper;
import com.RecipeBook.common.Ingredient;
import com.RecipeBook.common.Recipe;
import com.RecipeBook.common.RecipeActivity;
import com.RecipeBook.common.RecipeInProgress;
import com.RecipeBook.common.RecipeStep;
import com.RecipeBook.main.GlobalsManager;
import com.RecipeBook.main.R;
import com.RecipeBook.search.SearchResults;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;

public class CookingRecipe extends	RecipeActivity {
	DataBaseHelper myDbHelper;
	Recipe r;
	RecipeInProgress rip;
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.cooking_recipe);
		
		//myDbHelper = new DataBaseHelper(this);
		//myDbHelper.openDataBase();
		myDbHelper = GlobalsManager.getGlobals().getDatabaseHelper();
		
		int index = getIntent().getIntExtra("index", 0);
		rip = GlobalsManager.getGlobals().getRecipesInProgress().get(index);
		r = rip.getRecipe();
		
		TextView tv = (TextView)findViewById(R.id.TextView01);
		tv.setText(r.getName());
		
		ImageView iv = (ImageView)findViewById(R.id.ImageView01);
		try {
			Bitmap bitmap = BitmapFactory.decodeStream((InputStream)new URL(r.getImageFile()).getContent());
			iv.setImageBitmap(bitmap); 
		} catch (MalformedURLException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		
		Button button = (Button) findViewById(R.id.Button01);
		button.setOnClickListener(ingredientClickListener);
		
		ListView lv=(ListView)findViewById(R.id.ListView01);
		lv.setAdapter(new StepAdapter(this, android.R.layout.simple_list_item_1, r.getSteps()));
		lv.setOnItemClickListener(stepClickListener);
	}
	
	private class StepAdapter extends ArrayAdapter<RecipeStep> {
		
		private ArrayList<RecipeStep> steps;

		public StepAdapter(Context context, int textViewResourceId,
				List<RecipeStep> objects) {
			super(context, textViewResourceId, objects);
			steps = new ArrayList<RecipeStep>(objects);
		}
		
		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			View v = convertView;
			if (v == null) {
				LayoutInflater vi = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
				v = vi.inflate(R.layout.cooking_step, null);
			}
			RecipeStep step = steps.get(position);
			if (step != null) {
				TextView text = (TextView) v.findViewById(R.id.TextView01);
				if (text != null) {
					text.setText(step.getNumber()+") "+step.getText());
				}
				ImageView image = (ImageView) v.findViewById(R.id.ImageView01);
				if (image != null) {
					image.setImageResource(step.isCompleted() ? R.drawable.menu_tick : R.drawable.menu_circle);
				}
			}
			return v;
		}
	}
	
	private void showIngredients() {
			Intent i = new Intent(this, IngredientList.class);
			ArrayList<String> names = new ArrayList<String>();
			for (Ingredient ing : r.getIngredients()) {
				names.add(ing.getName());
			}
			i.putStringArrayListExtra("names",names);
			//i.putIntArrayExtra("amounts",amounts);
			//i.putStringArrayExtra("units",units);
			
			startActivity(i);
	}
	
	private OnClickListener ingredientClickListener = new OnClickListener() {

		public void onClick(View v) {
			showIngredients();
		}
	};

	private OnItemClickListener stepClickListener = new OnItemClickListener() {

		public void onItemClick(AdapterView<?> av, View v, int position, long id) {
			RecipeStep step = (RecipeStep)av.getItemAtPosition(position);
			step.setCompleted(!step.isCompleted());

			ImageView image = (ImageView) v.findViewById(R.id.ImageView01);
			if (image != null) {
				image.setImageResource(step.isCompleted() ? R.drawable.menu_tick : R.drawable.menu_circle);
			}
		}
	};
}